<template>
    <!-- ChildrenDict对话框 -->
    <el-dialog
      :title="!addChildrenDictForm.dictId ? '新增' : '修改'"
      :visible.sync="addChildrenDictdialog"
      width="50%">
      <el-form :model="addChildrenDictForm" :rules="addChildrenDictFormRules" ref="addChildrenDictFormRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属分组" >
              <el-select v-model="addChildrenDictForm.dictTypeId" placeholder="请选择分组">
                <el-option
                  v-for="item in options"
                  :key="item.dictTypeId"
                  :label="item.dictTypeName"
                  :value="item.dictTypeId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="输入值类型" prop="inputType">
              <el-select v-model="addChildrenDictForm.inputType" placeholder="" style="width:100%">
                <el-option
                  v-for="item in inputType"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-form-item label="字典名称" >
          <el-input v-model="addChildrenDictForm.dictName"></el-input>
        </el-form-item>
        <el-form-item label="字典编码" >
          <el-input v-model="addChildrenDictForm.code"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="排序号" >
              <el-input-number v-model="addChildrenDictForm.sort" :min="1" :max="100" label="排序号"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="状态" >
              <el-switch
                v-model="addChildrenDictForm.status"
                active-color="#409eff"
                inactive-color="#dcdfe6"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="默认字典" prop="isDefault">
              <el-switch
                v-model="addChildrenDictForm.isDefault"
                active-color="#409eff"
                inactive-color="#dcdfe6">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" >
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="addChildrenDictForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addChildrenDictdialog = false">取 消</el-button>
        <el-button type="primary" @click="submitChildrenDict">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import {
  postChildrenDict,
  editChildrenDict,
  getParentDictTable
} from '@/api/auth/dict.js'
export default {
  data() {
    return {
      addChildrenDictdialog: false,
      addChildrenDictForm: {
        dictName: '',
        dictId: '',
        code: '',
        status: 1,
        description: '',
        // dictTypeName: '',
        dictTypeId: '',
        sort: 1,
        isDefault: 0
        // inputType: 'text'
      },
      addChildrenDictFormRules: {
        dictName: [
          { required: true, message: '请输入分组名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入分组编码', trigger: 'blur' }
        ]
      },
      value: '',
      options: []
      // inputType: [
      //   { name: '文本', value: 'text' },
      //   { name: '多行文本', value: 'textare' },
      //   { name: '时间', value: 'time' },
      //   { name: '下拉框', value: 'select' }
      // ]
    }
  },
  created() {
    this.getTable()
  },
  methods: {
    // 获取数据字典的所有数据
    getTable() {
      getParentDictTable().then((res) => {
        this.options = res.data
      })
    },
    init(val, flag) {
      if (val.dictId) { // 修改
        this.addChildrenDictForm = val
      } else { // 新增
        this.addChildrenDictForm.dictId = ''
        this.clearChildrenForm()
      }
      this.addChildrenDictdialog = flag
    },
    clearChildrenForm() {
      this.addChildrenDictForm.dictTypeId = ''
      this.addChildrenDictForm.dictName = ''
      this.addChildrenDictForm.description = ''
      this.addChildrenDictForm.code = ''
      this.addChildrenDictForm.sort = 1
      this.addChildrenDictForm.isDefault = 0
    },
    submitChildrenDict() {
      this.$refs.addChildrenDictFormRef.validate(valid => {
        if (!valid) return
        const data = {
          dictName: this.addChildrenDictForm.dictName,
          dictId: this.addChildrenDictForm.dictId,
          code: this.addChildrenDictForm.code,
          status: this.addChildrenDictForm.status,
          description: this.addChildrenDictForm.description,
          dictTypeId: this.addChildrenDictForm.dictTypeId,
          sort: this.addChildrenDictForm.sort,
          isDefault: this.addChildrenDictForm.isDefault
        }
        if (!this.addChildrenDictForm.dictId) {
          postChildrenDict(data).then(res => {
            const dictTypeid = this.addChildrenDictForm.dictTypeId
            this.$emit('refreshChildrenDataList', dictTypeid)
            this.addChildrenDictdialog = false
          })
        } else {
          editChildrenDict(data).then(res => {
            this.$emit('refreshChildrenDataList', this.addChildrenDictForm.dictTypeId)
            this.addChildrenDictdialog = false
          })
        }
      })
    }
  }
}
</script>

<style>
</style>
